/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

/**
 * <p>Project: jse2204 - FileWriterDemo
 * <p>Powered by webrx On 2022-09-16 14:55:55
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class FileWriterDemo {
    public static void main(String[] args) {
        //实现将字符串保存到文件中。
        String str = "hello 中文效果 ".concat(String.format("%tF %<tT", System.currentTimeMillis()));
        System.out.println(str);

        String f = "user.txt";
        //java1.7 try with
        try (FileWriter fw = new FileWriter(f)) {
            fw.write(str);
        } catch (Exception e) {
            e.printStackTrace();
        }


        //传统处理异常的代码实现
        FileWriter fw = null;
        try {
            fw = new FileWriter(f);
            fw.write(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            fw.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
